package com.masterclient.app.businessmodule.minemodule.redpack.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.fragment.FCBaseFragment;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.common.config.BusinessConfig;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.redpack.detail.RedPackedDetailActivity;
import com.masterclient.app.businessmodule.minemodule.redpack.detail.RedPackedDetailTaskActivity;
import com.masterclient.app.businessmodule.minemodule.redpack.adapter.RedPackedFromAdapter;
import com.masterclient.app.modules.coupon.beans.RedPackedListBean;
import com.masterclient.app.businessmodule.minemodule.redpack.contract.RedPackedContract;
import com.masterclient.app.businessmodule.minemodule.redpack.contract.RedPackedPresenter;

import java.util.ArrayList;
import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/8/7 13:46
 * 功能说明：
 */

public class RedPackedFromFragment extends FCBaseFragment<RedPackedPresenter> implements RedPackedContract.View  {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    private RedPackedFromAdapter adapter;
    private int total;
    View emptyView;

    public static RedPackedFromFragment getInstance() {
        return new RedPackedFromFragment();
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.fragment_gzg_coin;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new RedPackedFromAdapter();
        recyclerView.setAdapter(adapter);
        adapter.bindToRecyclerView(recyclerView);
        emptyView = View.inflate(getContext(),R.layout.empty_hb,null);
        emptyView.setVisibility(View.GONE);
        TextView tvTip = (TextView) emptyView.findViewById(R.id.tv_tip);
        tvTip.setText("您还没有向日葵币收入记录哦~");
        TextView tvDesc = (TextView) emptyView.findViewById(R.id.tv_desc);
        tvDesc.setText("即使现在囊空如洗，我也能看到未来的富有~");
        adapter.setEmptyView(emptyView);
        mPresenter.getRedPackedFrom("1", BusinessConfig.PAGE_SIZE);
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(mPresenter.fromList!=null){
                    mPresenter.fromList.clear();
                    adapter.notifyDataSetChanged();
                }
                emptyView.setVisibility(View.GONE);
                adapter.setEnableLoadMore(true);
                mPresenter.getRedPackedFrom("1",BusinessConfig.PAGE_SIZE);
            }
        });

        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if(mPresenter.fromList.size()<total){
                    mPresenter.getRedPackedFrom(String.valueOf(mPresenter.fromList.size()/Integer.valueOf(BusinessConfig.PAGE_SIZE)+1),BusinessConfig.PAGE_SIZE);
                }
            }
        }, recyclerView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (Integer.parseInt(mPresenter.fromList.get(position).getSh_page_type())<4) {
                    startActivitryWithBundle(RedPackedDetailActivity.class, new String[][]{{"id", mPresenter.fromList.get(position).getSh_id()}, {"title_type", "0"}});
                }else {
                    startActivitryWithBundle(RedPackedDetailTaskActivity.class, new String[][]{{"id", mPresenter.fromList.get(position).getSh_id()}, {"title_type", "0"},{"type", mPresenter.fromList.get(position).getSh_page_type()}});
                }
            }
        });

    }


    public void setOnDataListerner(OnDataListerner onChooseListerner) {
        this.onDataListerner = onChooseListerner;
    }

    private OnDataListerner onDataListerner;

    public interface OnDataListerner{
        void getData(String bupiao,String url);
    }

    @Override
    public void redPackedFrom(RedPackedListBean.DataBean dataBean) {

        if(swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }

        total = Integer.parseInt(dataBean.getSh_total_rows());
        if(mPresenter.fromList == null){
            mPresenter.fromList = new ArrayList<>();
        }
        mPresenter.fromList.addAll(dataBean.getSh_items());

        adapter.setNewData(mPresenter.fromList);

        if(mPresenter.fromList.size()>=total){
            adapter.setEnableLoadMore(false);
        }
        emptyView.setVisibility(View.VISIBLE);
        if(onDataListerner!=null){
            if(!TextUtils.isEmpty(dataBean.getSh_bupiao())){
                onDataListerner.getData(dataBean.getSh_bupiao(),dataBean.getSh_bupiao_introduce());
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void redPackedpayFor(RedPackedListBean.DataBean dataBean) {

    }

    @Override
    public void loadFailed() {
        if(swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }
        if (mPresenter.fromList != null) {
            adapter.loadMoreFail();
        }

//        emptyView.setVisibility(View.VISIBLE);
    }
}
